--
-- Author: shunguo.chen
-- Date: 2017-09-11 11:32:35
--
-- ZoneView	服务器列表

local ScrollViewExtend = require("app.widget.ScrollViewExtend")
local ZoneListRender = import(".ZoneListRender")
local ZoneRender = import(".ZoneRender")
local FriendZoneListRender = import(".FriendZoneListRender")
local ListViewVertical = require("app.widget.listview.ListViewVertical")
local fileName = string.getModeName(...)
local ZoneViewEma = class(fileName, app.mvc.ViewBase)

ZoneViewEma.VIEW_ROOT_RES = Res.LoginZoneEmaScene

function ZoneViewEma:init()
	self._ui.Button_back:addClickEventListener(function()
		executeFunc(self._onOperate, "exit")
	end)

	self._curListRender = nil
	self._list = self._ui.ListView
	self._list.createRender = function (list, idx)
		local items = list:getItems()
    	local render = items[idx]
    	if nil == render then
			render = ZoneListRender.create()
			list:pushBackCustomItem(render)
		end
		render:setVisible(true)
		return render
	end

	self._curZoneRender = nil
	self._sv = ScrollViewExtend.create(self._ui.ScrollView, {class=ZoneRender})
	self:initTabBtns()
end

function ZoneViewEma:show()
	self.super.show(self)
	local render = nil
	if self._datas then
		local curSelectIndex = self._cusSelectIndex or 1
		if not self._datas[curSelectIndex] then
			curSelectIndex = 1
		end
		render = self._list:getItem(curSelectIndex -1)
		self:_setCurListRender(render)
		local selectTag = self._datas[curSelectIndex]
		if selectTag then
			executeFunc(self._onOperate, "selectList", self._datas[curSelectIndex])
		end
	else
		self:_setCurListRender()
	end

end

function ZoneViewEma:_setCurListRender(render)
	if self._curListRender then
		self._curListRender:setSelected(false)
	end
	self._curListRender = render or self._list:getItem(0)
	if self._curListRender then
		self._curListRender:setSelected(true)
	end
end

function ZoneViewEma:refreshList(datas)
	local selectName
	self._datas = datas
	local items = self._list:getItems()
	for k, v in pairs(items) do
		v:setVisible(false)
	end
	local curSelectIndex = self._cusSelectIndex or 1
	for i,data in ipairs(datas) do
		local render = self._list:createRender(i)
		render:setData(data)
		render:onBtnClick(function(render)
			local data = render:getData()
			self:_setCurListRender(render)
			self._cusSelectIndex = i
			executeFunc(self._onOperate, "selectList", data)
		end)
	end
	if not datas[curSelectIndex] then
		curSelectIndex = 1
	end
	if datas[curSelectIndex] then
		self._cusSelectIndex = curSelectIndex
		local render = self._list:getItem(curSelectIndex -1)
		self:_setCurListRender(render)
		executeFunc(self._onOperate, "selectList", datas[curSelectIndex])
	end
end


function ZoneViewEma:refreshZones(datas)
	if datas then
		if datas.isFriend then
			self._sv:setVisible(false)
		else
			self._sv:setVisible(true)
			self._sv:showDataList(datas, 0, function(render)
				local data = render:getData()
				executeFunc(self._onOperate, "selectZone", data)
			end)
		end
	end
end


function ZoneViewEma:updateSelectTab()
	for k, v in pairs(self._tabConfig) do
		if k == self._curSelectTabIndex then
			v["nameLabel1"]:setVisible(false)
			v["nameLabel2"]:setVisible(true)
			v["btn"]:setTouchEnabled(false)
			v["btn"]:setBright(false)
		else
			v["nameLabel1"]:setVisible(true)
			v["nameLabel2"]:setVisible(false)
			v["btn"]:setTouchEnabled(true)
			v["btn"]:setBright(true)
		end
	end
end

function ZoneViewEma:_onTabSelect(index)
	print("_onTabSelect  ", index)
	self._curSelectTabIndex = index
	self:updateSelectTab()
	executeFunc(self._onOperate, "selectZoneByTag", self._tabConfig[index]["key"])
end

function ZoneViewEma:initTabBtns()
	local tabConfig = {
		{name = L("EmaZoneListTab1"), key = "99"},
		{name = L("EmaZoneListTab2"), key = "UTC+08:00"},
		{name = L("EmaZoneListTab3"), key = "UTC+01:00"},
		{name = L("EmaZoneListTab4"), key = "UTC-05:00"}
	}
	self._tabConfig = tabConfig
	self._curSelectTabIndex = 1
	local panelTab = self._ui["Panel/Panel_tab"]
	for i = 1, 4 do
		local btn = self._ui["Panel/Panel_tab/selected_btn_"..i]
		local name1 = btn:getChildByName("name_1")
		local name2 = btn:getChildByName("name_2")
		tabConfig[i]["btn"] = btn
		tabConfig[i]["nameLabel1"] = name1
		tabConfig[i]["nameLabel2"] = name2
		name1:setString(tabConfig[i]["name"])
		name2:setString(tabConfig[i]["name"])
		btn:addClickEventListener(function()
			self:_onTabSelect(i)
		end)
	end
	
	self:updateSelectTab()
	
end

function ZoneViewEma:remove()
	self._sv:remove()
end

return ZoneViewEma